package com.example.yls.demoa;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SearchActivity extends AppCompatActivity {
    private EditText edtNo;
    private Button btnSearch;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        edtNo = findViewById(R.id.edt_no);
        btnSearch = findViewById(R.id.btn_search);

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MyDataBaseHelper helper = new MyDataBaseHelper(SearchActivity.this);
                SQLiteDatabase db = helper.getReadableDatabase();
                String key = edtNo.getText().toString();
                String where = "no = " + key;
                Cursor cursor = db.query("student",null, where,null,null,null,null);
                Log.e("SearchActivity", "count= " + cursor.getCount());
                cursor.moveToFirst();
                String name = cursor.getString(2);
                String birth = cursor.getString(3);
                String phone  = cursor.getString(4);

                Intent intent = new Intent();
                intent.putExtra("NO", key);
                intent.putExtra("NAME", name);
                intent.putExtra("BIRTH", birth);
                intent.putExtra("PHONE", phone);
                intent.setClass(SearchActivity.this, StudentActivity.class);
                startActivity(intent);
            }
        });
    }
}
